package com.takeoff.iot.modbus.netty.handle;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class ServerHeartbeatHandler extends IdleStateHandler {

    public ServerHeartbeatHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit) {
        super(readerIdleTime, writerIdleTime, allIdleTime, unit);
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        // 指定时间内没有读事件发送 就会触发 IdleState.READER_IDLE 类型事件
        // 我们就可以对该连接进行处理 这里是直接关闭
        if(evt.state()== IdleState.READER_IDLE){
            log.info("{} 秒内没有读取到数据,关闭连接", 60);
            ctx.channel().close();
        }
    }

}
